﻿using System;
using System.Collections.Generic;
using IterationLife.Datas;
using IterationLife.FootballTeaching.Models;
using IterationLife.FootballTeaching.Queries;
using IterationLife.FootballTeaching.Repositories;
using IterationLife.PlatformBackEnd.AppServices.Contracts.FootballTeachings;
using IterationLife.PlatformBackEnd.AppServices.Dtos.FootballTeachings;
using Util;
using Util.Applications;
using Util.Datas.Queries;
using Util.Exceptions;

namespace IterationLife.PlatformBackEnd.AppServices.Impl.FootballTeachings
{
    /// <summary>
    /// 球员位置服务
    /// </summary>
    public class PlayerPositionService : ApplicationServiceBase<PlayerPosition, PlayerPositionDto, PlayerPositionQuery>, IPlayerPositionService
    {
        /// <summary>
        /// 初始化球员位置服务
        /// </summary>
        /// <param name="unitOfWork">工作单元</param>
        /// <param name="playerPositionRepository">球员位置仓储</param>
        public PlayerPositionService(IApplicationUnitOfWork unitOfWork, IPlayerPositionRepository playerPositionRepository)
            : base(unitOfWork, playerPositionRepository)
        {
            PlayerPositionRepository = playerPositionRepository;
        }

        /// <summary>
        /// 球员位置仓储
        /// </summary>
        protected IPlayerPositionRepository PlayerPositionRepository { get; set; }

        /// <summary>
        /// 转换为数据传输对象
        /// </summary>
        /// <param name="entity">实体</param>
        protected override PlayerPositionDto ToDto(PlayerPosition entity)
        {
            return entity.ToDto();
        }

        /// <summary>
        /// 转换为实体
        /// </summary>
        /// <param name="dto">数据传输对象</param>
        protected override PlayerPosition ToEntity(PlayerPositionDto dto)
        {
            return dto.ToEntity();
        }

        /// <summary>
        /// 创建查询对象
        /// </summary>
        /// <param name="param">球员位置查询实体</param>
        protected override IQueryBase<PlayerPosition> CreateQuery(PlayerPositionQuery param)
        {
            var excludeIds = param.ExcludeIds.IsEmpty() ? new List<Guid>() : param.ExcludeIds.ToGuidList();
            return new Query<PlayerPosition>(param)
                .WhereIf(t => excludeIds.Contains(t.Id) == false, excludeIds.Count != 0)
                .Filter(t => t.Enabled == param.Enabled)
                .Filter(t => t.Code.Contains(param.Code))
                .Filter(t => t.Name.Contains(param.Name));
        }


        /// <summary>创建实体</summary>
        public override PlayerPositionDto Create()
        {
            var dto = base.Create();
            return dto.Init(PlayerPositionRepository.GetSortId());
        }

        /// <summary>保存前操作</summary>
        /// <param name="dto">数据传输对象</param>
        protected override void SaveBefore(PlayerPositionDto dto)
        {
            base.SaveBefore(dto);
            if (PlayerPositionRepository.IsRepeat(dto.Code, dto.Name, dto.Id))
                throw new Warning("编码或名称已存在");
        }

    }
}